<template>
  <div class="home">
    <el-container>
      <el-aside width="auto">
        <div class="logo"></div>
        <el-menu
        :unique-opened="true"
          default-active="2"
          :collapse="isCollapse"
          class="el-menu-admin"
          background-color="#F9F9F9"
          active-text-color="#409EFF"
          :router="true"
          @open="handleOpen"
          @close="handleClose">
          <el-submenu :index="mainItem.authName" v-for="(mainItem, index) in leftMenus" :key="index">
            <template slot="title">
              <i class="el-icon-location"></i>
              <span>{{mainItem.authName}}</span>
            </template>
            <el-menu-item :index="subItem.path" v-for="(subItem, index) in mainItem.children" :key="index">
              <i class="el-icon-menu"></i>
              <span slot="title">{{subItem.authName}}</span>
            </el-menu-item>
          </el-submenu>
          <!-- <el-submenu index="2">
            <template slot="title">
              <i class="el-icon-location"></i>
              <span>权限管理</span>
            </template>
            <el-menu-item index="/roles">
              <i class="el-icon-menu"></i>
              <span slot="title">角色列表</span>
            </el-menu-item>
            <el-menu-item index="/rights">
              <i class="el-icon-menu"></i>
              <span slot="title">权限列表</span>
            </el-menu-item>
          </el-submenu>
          <el-submenu index="3">
            <template slot="title">
              <i class="el-icon-location"></i>
              <span>商品管理</span>
            </template>
            <el-menu-item index="/productlist">
              <i class="el-icon-menu"></i>
              <span slot="title">商品列表</span>
            </el-menu-item>
            <el-menu-item index="/sortingparams">
              <i class="el-icon-menu"></i>
              <span slot="title">分类参数</span>
            </el-menu-item>
            <el-menu-item index="/productcategory">
              <i class="el-icon-menu"></i>
              <span slot="title">商品分类</span>
            </el-menu-item>
          </el-submenu> -->
        </el-menu>
      </el-aside>

      <el-container>
        <el-header>
          <i class="myicon myicon-menu toggle-btn" @click="isCollapse = !isCollapse"></i>
          <div class="system-title">电商后台管理系统</div>
          <div>
            <span class="welcome">你好,{{$store.getters.gettersName}}</span>
            <el-button type="warning" round @click="logOut">退出</el-button>
          </div>
        </el-header>
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import {getLeftMenus} from '@/api'
export default {
  data () {
    return {
      isCollapse: false,
      leftMenus: []
    }
  },
  created () {
    getLeftMenus().then(res => {
      // console.log(res)
      this.leftMenus = res.data
    })
  },
  methods: {
    handleOpen (key, keyPath) {
      // console.log(key, keyPath)
    },
    handleClose (key, keyPath) {
      // console.log(key, keyPath)
    },
    toggleCollapse () {
      // this.isCollapse = !this.isCollapse
    },
    logOut () {
      // localStorage.removeItem('adminToken')
      // this.$router.push({name: 'login'})
      this.$confirm('确认退出该账号?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        localStorage.removeItem('adminToken')
        this.$router.push({name: 'login'})
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消退出'
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.home {
  height: 100%;
  .el-menu-admin:not(.el-menu--collapse) {
    width: 200px;
    min-height: 400px;
  }
  .el-container {
    height: 100%;
  }
  .el-aside {
    background-color: #F9F9F9;
  }
  .el-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background-color: #409EFF;
  }
  .logo {
    height:60px;
    background: url(../assets/logo.png);
    background-size: cover;
    background-color: white;
  }
  .toggle-btn {
    padding: 0 15px;
    margin-left: -20px;
    font-size: 36px;
    color: white;
    cursor: pointer;
    line-height: 60px;
    &:hover {
      background-color: #4292CF;
    }
  }
  .system-title {
    font-size: 28px;
    color: white;
  }
  .welcome {
    color: white;
  }
}
</style>
